package main.remote;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import main.Executor;
import main.Task;

/**
 * Currently we support only one remote host in addition to the local host.
 * This will be extended for many remote hosts
 *
 */
public class RemoteExecutor implements Executor {
	//public static final String HOST = "localhost";
	private final List<String> hosts = new ArrayList<String>();
	public static final int PORT = 1998;
	
	public RemoteExecutor() {
		// Create a list of remote hosts to execute tasks
		this.hosts.add("localhost"); // Currently only 1 remote host
	}
	
	@Override
	public boolean execute(Task task) {
		String host = hosts.get(0);
		Client conn = null;
		try {
			conn = new Client(host, PORT);
		} catch(Exception e) {
			System.err.println("Unable to connect: " + e);
			return false;
		}
		Message mOut = new Message();
		mOut.setType(TaskService.TASK_SERVICE_MESSAGE);
		mOut.setParam("cmd_name", task.getCommand().getName());
		mOut.setIntegerParam("cmd_arg", task.getCommand().getArgument());

		Message mIn = conn.call(mOut);
		conn.disconnect();
		
		return true;
	}
	
	@Override
	public void shutdown() {
		// TODO Nothing to be done now
	}
		
}
